home *** CD-ROM | disk | FTP | other *** search
/ A.C.E. 2 / ACE CD 2.iso / FILES / UTILS / AMOSPRO4.DMS / in.adf / Tutorials / Hardware_Sprites.AMOS / Hardware_Sprites.amosSourceCode
Encoding:
AMOS Source Code  |  1992-09-28  |  12.4 KB  |  354 lines

  1. '*************************** 
  2. '*    AMOS Professional    * 
  3. '*                         * 
  4. '*    HARDWARE SPRITES     * 
  5. '*                         * 
  6. '* (c) Europress Software  * 
  7. '*                         * 
  8. '*     Ronnie Simpson      * 
  9. '*************************** 
  10. '
  11. '------------------- 
  12. 'GENERAL 
  13. '------------------- 
  14. 'The Amiga supports a total of 8 hardware sprites which are 16 pixels wide 
  15. 'and up to 255 pixels high.
  16. '
  17. 'The hardware sprites are numbered 0-7 but since it is very unlikely that
  18. 'you will ever need a sprite which is 255 pixels high, each of the hardware  
  19. 'sprites can be split into sections allowing up to 64 sprites to be displayed. 
  20. 'These computed sprites which use the numbers 8-63 are dealt with in the 
  21. 'second of the sprite tutorials. 
  22. '
  23. 'A single hardware sprite can only be displayed using up to 3 colours (colour
  24. '(0 is treated as transparent) but by overlaying two hardware sprites then 
  25. 'up to 15 colours can be used, this obviously reduces the total number that  
  26. 'can be displayed. 
  27. '
  28. 'The same is also true for sprites which use images greater than sixteen 
  29. 'pixels wide, the sprites required to be combined are 'grabbed' by the   
  30. 'hardware and do not require any further software or user control. 
  31. '
  32. 'Sprites are independant from the screen and are automatically overlaid
  33. 'in any screen mode including HAM and Extra Half Bright. 
  34. '
  35. '------------------- 
  36. 'SPRITE BASICS 
  37. '------------------- 
  38. '                 +---->(sprite identification number) 
  39. '                 |  +---->(hardware x coordinate) 
  40. '                 |  |   +---->(hardware y coordinate) 
  41. '                 |  |   |  +---->(image number to be used)
  42. '                 |  |   |  |
  43. '          Sprite 1,200,150,5
  44. '
  45. 'Sprite positions are measured in hardware coordinates from the image hot
  46. 'spot, these hardware coordinates are independant of the screen mode and   
  47. 'the functions X Hard, Y Hard and X Screen, Y Screen are provided for  
  48. 'converting between the different modes. 
  49. '
  50. 'Any number of parameters can be ommited from the Sprite instruction as long 
  51. 'as the commas are included in the correct place.
  52. '
  53. 'Sprites use the same images as the bobs and any number of sprites can use 
  54. 'the same image number, it is also possible to grab the image required from  
  55. 'the existing screen graphics using the Get Sprite instruction.
  56. '
  57. 'eg.   Get Sprite 1,10,10 To 26,26  (grab image number 1 from current screen)
  58. '
  59. 'By default hardware sprite number 0 is used by the mouse pointer but this 
  60. 'can be released for use by the Hide instruction.
  61. '
  62. '------------------- 
  63. 'SPRITE LIMITATIONS
  64. '------------------- 
  65. 'A maximum width of 8*16=128 pixels is possible if all sprites are joined
  66. 'together in the 3 colour mode.
  67. '
  68. 'A maximum width of 4*16=64 pixels is possible if all sprites are joined 
  69. 'together in the 15 colour mode. 
  70. '
  71. 'When using 32 colour,HAM or Extra Half Bright screen modes setting the
  72. 'colours for your 3 colour sprites can be a problem as the hardware sprites  
  73. 'use the colour indexes 16 to 31.
  74. '
  75. 'The hardware sprites are grouped in pairs as follows:-
  76. '
  77. '   numbers 0 and 1 use colour indexes 17,18,19  (16 treated as transparent) 
  78. '   numbers 2 and 3 use colour indexes 21,22,23  (20 treated as transparent) 
  79. '   numbers 4 and 5 use colour indexes 25,26,27  (24 treated as transparent) 
  80. '   numbers 6 and 7 use colour indexes 29,30,31  (28 treated as transparent) 
  81. '
  82. 'When joining sprites the computer automatically grabs the images using the
  83. 'above pairs so always try to give your 15 colour (or wide sprites) even 
  84. 'numbers to avoid wasting sprites. 
  85. '
  86. 'The Get Sprite Palette instruction loads the entire palette from the sprite 
  87. 'bank and an optional mask can be used to load only a selection. 
  88. '
  89. 'eg      Get Sprite Palette         (load entire palette)  
  90. '        Get Sprite Palette %1010   (load only some colours) 
  91. '
  92. 'If the optional mask is used then each of the 32 colour registers are 
  93. 'represented by one bit which can be either set or clear, if set then  
  94. 'that colour will be loaded. 
  95. '
  96. '------------------- 
  97. 'OTHER SPRITE INSTRUCTIONS 
  98. '------------------- 
  99. 'Get Sprite
  100. '
  101. 'grab a section of the screen to the sprite bank 
  102. '
  103. 'eg.      Get Sprite 1,10,20 To 30,40  
  104. '
  105. 'The above would grab image number 1 from the current screen from the
  106. 'given coordinates.
  107. '
  108. 'An optional screen number can be added BEFORE the image number if you wish
  109. 'to grab from another opened screen
  110. '
  111. 'eg.      Get Bob 2,1,10,20 To 30,40   (grab from screen number 2) 
  112. '
  113. 'Hot Spot
  114. '
  115. 'The hot spot of an object is used as the reference point for coordinate 
  116. 'calculations. There are two versions of the Hot Spot instruction the first  
  117. 'of which moves the hot spot to one of nine pre-defined positions:-  
  118. '
  119. '                            +--->number of sprite image in the bank 
  120. '                            |  +--->new position of Hot Spot
  121. '                   Hot Spot 1,&00 
  122. '                          +-------------+   
  123. '     here are the nine    |$00  $10  $20|     
  124. '     possible positions   |$01  $11  $21|       
  125. '                          |$02  $12  $22|     
  126. '                          +-------------+   
  127. '
  128. 'The second version of this instruction lets you position the hot spot 
  129. 'to an x,y position which may even lie outside the area of the bob, all
  130. 'measurements are from the top left hand corner. 
  131. '
  132. '                            +--->number of sprite image in the bank 
  133. '                            |  +--->the new position of Hot Spot
  134. '                   Hot Spot 1,x,y 
  135. '
  136. 'To save a little memory sprites can be set to a maximum height using the  
  137. 'Set Sprite Buffer instruction. The size can be set between 16 and 256   
  138. 'pixels and the size of the buffer is calculated by multiplying the height 
  139. 'required by 96. 
  140. '
  141. 'eg      Set Sprite Buffer 1536   (set max. height to 16 pixels) 
  142. '
  143. 'To remove one or all sprites from the display use the Sprite Off instruction. 
  144. '
  145. 'eg.      Sprite Off    (no parameters removes all sprites)  
  146. '         Sprite Off 1  (remove sprite number 1) 
  147. '
  148. 'Two functions are provided for returning a sprites coordinates X sprite and 
  149. 'Y Sprite. 
  150. '
  151. 'eg.       X=X Sprite(2)  (get hardware X coordinate of sprite number 2) 
  152. '          Y=Y Sprite(4)  (get hardware Y coordinate of sprite number 4) 
  153. '
  154. 'The function I Sprite returns the current image number that is being used 
  155. 'by a particular sprite. 
  156. '
  157. 'eg.           I=I Sprite(5) 
  158. '
  159. 'This function can be usefull for synchronising sound with   
  160. 'sprite animations etc.  
  161. '
  162. '------------------- 
  163. 'FINAL NOTE
  164. '------------------- 
  165. 'A 3 colour sprite is a sprite which uses only 2 bitplanes, so when grabbing 
  166. 'your images make sure that the screen mode is 4 colour or less or you may 
  167. 'inadvertantly grab an image which requires more colours (or bitplanes), this
  168. 'would result in the sprite being displayed as a 15 colour with the resulting
  169. 'loss of a sprite. 
  170. '
  171. '------------------- 
  172. 'WORKING EXAMPLES
  173. '------------------- 
  174. Rem *** dimension arrays which hold coordinates and directions 
  175. '
  176. Dim X(7),Y(7),XD(7),YD(7)
  177. Global X(),Y(),XD(),YD()
  178. '
  179. '
  180. Rem *** load some sprite images
  181. '
  182. Load "AMOSPro_Tutorial:Objects/Sprites.abk"
  183. '
  184. '
  185. Rem *** tidy up screen 
  186. '
  187. Screen Open 0,640,200,4,Hires : Flash Off 
  188. Palette $0,$F00,$F0,$F
  189. Curs Off : Cls 0 : Paper 0 : Pen 2
  190. Reserve Zone 6
  191. '
  192. '
  193. Rem *** set up some buttons
  194. '
  195. Locate 0,1 : Centre Border$(Zone$("EIGHT 3 COLOURED SPRITES",1),1)
  196. Locate 0,5 : Centre Border$(Zone$("FOUR 15 COLOURED SPRITES",2),1)
  197. Locate 0,9 : Centre Border$(Zone$("ONE MAX. WIDTH 3 COLOURED SPRITE",3),1)
  198. Locate 0,13 : Centre Border$(Zone$("ONE MAX. WIDTH 15 COLOURED SPRITE",4),1)
  199. Locate 0,17 : Centre Border$(Zone$("SPRITES ON A HAM SCREEN",5),1)
  200. Locate 0,21 : Centre Border$(Zone$("SPRITES ON A EXTRA HALF BRIGHT SCREEN",6),1)
  201. Pen 1 : Locate 0,24 : Centre "TAKE YOUR PICK WITH THE LEFT MOUSE KEY                  RIGHT MOUSE KEY TO QUIT"
  202. '
  203. '
  204. Rem *** start main loop
  205. '
  206. Do 
  207.    Repeat 
  208.    Until Mouse Key=0
  209.    Repeat 
  210.       M=Mouse Zone
  211.       If Mouse Key=2 Then Exit 2
  212.    Until Mouse Key=1 and M>0
  213.    On M Proc EIGHTX3,FOURX16,BIGX3,BIGX15,HAM,EXB
  214. Loop 
  215. '
  216. '
  217. Edit 
  218. '
  219. '
  220. Rem *** the sprite procedures
  221. '
  222. Procedure EIGHTX3
  223.    Screen Open 1,320,200,8,Lowres : Flash Off : Curs Off : Get Sprite Palette 
  224.    Set Pattern 24 : Ink 3,0 : Paint 1,1 : Hide 
  225.    Pen 3 : Locate 0,24 : Centre "press right mouse key to continue"
  226.    For N=0 To 7
  227.       X(N)=Rnd(260)+150 : Y(N)=Rnd(150)+50
  228.       Sprite N,X(N),Y(N),1
  229.       XD(N)=2 : If Rnd(1)=0 Then XD(N)=-XD(N)
  230.       YD(N)=2 : If Rnd(1)=0 Then YD(N)=-YD(N)
  231.    Next 
  232.    Do 
  233.       For N=0 To 7
  234.          Add X(N),XD(N) : If X(N)>410 or X(N)<150 Then XD(N)=-XD(N)
  235.          Add Y(N),YD(N) : If Y(N)>220 or Y(N)<50 Then YD(N)=-YD(N)
  236.          Sprite N,X(N),Y(N),1
  237.       Next 
  238.       Exit If Mouse Key=2
  239.    Loop 
  240.    Sprite Off : Screen Close 1 : Show On 
  241. End Proc
  242. Procedure FOURX16
  243.    Screen Open 1,320,200,16,Lowres : Flash Off : Curs Off : Get Sprite Palette 
  244.    Set Pattern 5 : Ink 3,0 : Paint 1,1 : Hide 
  245.    Pen 9 : Locate 0,24 : Centre "press right mouse key to continue"
  246.    For N=0 To 6 Step 2
  247.       X(N)=Rnd(260)+150 : Y(N)=Rnd(150)+50
  248.       Sprite N,X(N),Y(N),2
  249.       XD(N)=2 : If Rnd(1)=0 Then XD(N)=-XD(N)
  250.       YD(N)=2 : If Rnd(1)=0 Then YD(N)=-YD(N)
  251.    Next 
  252.    Do 
  253.       For N=0 To 6 Step 2
  254.          Add X(N),XD(N) : If X(N)>410 or X(N)<150 Then XD(N)=-XD(N)
  255.          Add Y(N),YD(N) : If Y(N)>220 or Y(N)<50 Then YD(N)=-YD(N)
  256.          Sprite N,X(N),Y(N),2
  257.       Next 
  258.       Exit If Mouse Key=2
  259.    Loop 
  260.    Sprite Off : Screen Close 1 : Show On 
  261. End Proc
  262. Procedure BIGX3
  263.    Screen Open 1,320,200,4,Lowres : Flash Off : Curs Off : Get Sprite Palette 
  264.    Set Pattern 30 : Ink 3,0,1 : Set Paint 1 : Box 0,0 To 127,95 : Paint 1,1 : Hide 
  265.    X(0)=Rnd(260)+150 : Y(0)=Rnd(150)+50
  266.    Get Sprite 7,0,0 To 128,96
  267.    Set Pattern 7 : Ink 3,0 : Cls 0 : Paint 1,1 : Hide 
  268.    Pen 3 : Locate 0,24 : Centre "press right mouse key to continue"
  269.    Sprite N,X(0),Y(0),7
  270.    XD(0)=2 : If Rnd(1)=0 Then XD(0)=-XD(0)
  271.    YD(0)=2 : If Rnd(1)=0 Then YD(0)=-YD(0)
  272.    Do 
  273.       Add X(0),XD(0) : If X(0)>410 or X(0)<130 Then XD(0)=-XD(0)
  274.       Add Y(0),YD(0) : If Y(0)>220 or Y(0)<50 Then YD(0)=-YD(0)
  275.       Sprite N,X(0),Y(0),7
  276.       Exit If Mouse Key=2
  277.       Wait Vbl 
  278.    Loop 
  279.    Sprite Off : Screen Close 1 : Show On 
  280. End Proc
  281. Procedure BIGX15
  282.    Screen Open 1,320,200,16,Lowres : Flash Off : Curs Off : Get Sprite Palette 
  283.    Set Pattern 9 : Ink 7,9 : Paint 1,1 : Hide 
  284.    X(0)=Rnd(260)+150 : Y(0)=Rnd(150)+50
  285.    Set Pattern 10 : Ink 3,0 : Cls 0 : Paint 1,1 : Hide 
  286.    Pen 11 : Locate 0,24 : Centre "press right mouse key to continue"
  287.    Sprite N,X(0),Y(0),6
  288.    XD(0)=2 : If Rnd(1)=0 Then XD(0)=-XD(0)
  289.    YD(0)=2 : If Rnd(1)=0 Then YD(0)=-YD(0)
  290.    Do 
  291.       Add X(0),XD(0) : If X(0)>410 or X(0)<130 Then XD(0)=-XD(0)
  292.       Add Y(0),YD(0) : If Y(0)>220 or Y(0)<50 Then YD(0)=-YD(0)
  293.       Sprite N,X(0),Y(0),6
  294.       Exit If Mouse Key=2
  295.       Wait Vbl 
  296.    Loop 
  297.    Sprite Off : Screen Close 1 : Show On 
  298. End Proc
  299. Procedure HAM
  300.    Screen Open 1,320,290,4096,Lowres
  301.    Curs Off : Cls 0 : Flash Off : Hide 
  302.    For B=0 To 15
  303.       Colour B,B : Ink B : Bar 32,B*7+32 To 288,B*7+39
  304.       For R=0 To 15
  305.          For L1=32 To 39 : Ink 32+R : Plot R*16+33,B*7+L1 : Next L1
  306.          For G=0 To 15
  307.             Ink 48+G : Draw R*16+G+34,B*7+32 To R*16+G+34,B*7+39
  308.          Next G
  309.       Next R
  310.    Next B
  311.    Pen 13 : Locate 0,24 : Centre "press right mouse key to continue"
  312.    For N=0 To 6 Step 2
  313.       X(N)=Rnd(260)+150 : Y(N)=Rnd(150)+50
  314.       Sprite N,X(N),Y(N),5
  315.       XD(N)=2 : If Rnd(1)=0 Then XD(N)=-XD(N)
  316.       YD(N)=2 : If Rnd(1)=0 Then YD(N)=-YD(N)
  317.    Next 
  318.    Do 
  319.       For N=0 To 6 Step 2
  320.          Add X(N),XD(N) : If X(N)>410 or X(N)<150 Then XD(N)=-XD(N)
  321.          Add Y(N),YD(N) : If Y(N)>220 or Y(N)<50 Then YD(N)=-YD(N)
  322.          Sprite N,X(N),Y(N),5
  323.       Next 
  324.       Exit If Mouse Key=2
  325.    Loop 
  326.    Sprite Off : Screen Close 1 : Show On 
  327. End Proc
  328. Procedure EXB
  329.    Screen Open 1,320,200,64,Lowres : Curs Off : Flash Off : Cls 0 : Paper 0 : Hide 
  330.    Palette 0,$FFF,$DDD,$BBB,$999,$F00,$D00,$B00,$900,$90,$B0,$D0,$F0,$9,$B,$D,$F
  331.    For X=0 To 319 Step 2
  332.       Add C,1,1 To 63
  333.       Ink C
  334.       Draw 0-X/2,0 To X,199
  335.       Ink C+32
  336.       Draw 0-X/2,0 To X+1,199
  337.    Next 
  338.    Pen 3 : Locate 0,24 : Centre "press right mouse key to continue"
  339.    For N=0 To 7
  340.       X(N)=Rnd(260)+150 : Y(N)=Rnd(150)+50
  341.       Sprite N,X(N),Y(N),1
  342.       XD(N)=2 : If Rnd(1)=0 Then XD(N)=-XD(N)
  343.       YD(N)=2 : If Rnd(1)=0 Then YD(N)=-YD(N)
  344.    Next 
  345.    Do 
  346.       For N=0 To 7
  347.          Add X(N),XD(N) : If X(N)>410 or X(N)<150 Then XD(N)=-XD(N)
  348.          Add Y(N),YD(N) : If Y(N)>220 or Y(N)<50 Then YD(N)=-YD(N)
  349.          Sprite N,X(N),Y(N),1
  350.       Next 
  351.       Exit If Mouse Key=2
  352.    Loop 
  353.    Sprite Off : Screen Close 1 : Show On 
  354. End Proc